﻿// Reference http://csharp.net-informations.com/communications/csharp-client-socket.htm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace SockClient
{
    public partial class Form1 : Form
    {
        TcpClient clientSocket = new TcpClient();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            statusText.Text = statusLabel.Text + Environment.NewLine + "Client started...";
            IPAddress[] address = Dns.GetHostAddresses("LocalHost");
            clientSocket.Connect(address[1], 8888);
            statusLabel.Text = "Server connected...";
        }

        private void SendDataToServer_Click(object sender, EventArgs e)
        {
            ////NetworkStream serverStream = clientSocket.GetStream();
            ////byte[] outStream = System.Text.Encoding.ASCII.GetBytes("Message from Client$");
            ////serverStream.Write(outStream, 0, outStream.Length);
            ////serverStream.Flush();

            ////byte[] inStream = new byte[10025];
            ////serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            ////string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            ////statusText.Text = statusText.Text + Environment.NewLine + "Data from Server : " + returndata;
        }

        private void SendXmlData_Click(object sender, EventArgs e)
        {
            System.IO.StreamReader stream = new System.IO.StreamReader("analysen.xml");
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(stream.ReadToEnd());
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            statusText.Text = statusText.Text + Environment.NewLine + "Data from Server : " + returndata;
        }
    }
}
